{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## incidence matrix of hypergraph\n",
    "\n",
    "credits: [DHNE](https://github.com/tadpole/DHNE)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "each triple is a hyperedge"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "each component type is a node\n",
    "\n",
    "e.g.: for a knowledge base, each subject is a node, each relation is a node and each object is a node"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### use [csr_matrix](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html) to store data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "compute incident matrix for each node type\n",
    "e.g.: for a knowledge base, there are three node types - subject type, relation type, object type\n",
    "'''\n",
    "from scipy.sparse import csr_matrix\n",
    "\n",
    "H = [] #incidence matrix\n",
    "E = nums_examples #each example is a hyperedge\n",
    "\n",
    "for i in range(3):\n",
    "    \n",
    "    data = np.ones(E)\n",
    "    row = edge[:, i]\n",
    "    col = range(E)\n",
    "    V = nums_type[i]\n",
    "    \n",
    "    csr_H = csr_matrix((data, (row, col)), shape=(V,E))\n",
    "    H.append(csr_H)\n",
    "    \n",
    "\n",
    "'''\n",
    "H = [csr_matrix((np.ones(nums_examples), \n",
    "                 (edge[:, i], range(nums_examples))), \n",
    "                shape=(nums_type[i], num_hyperedges)) for i in range(3)]\n",
    "'''"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
